package config

import (
	"encoding/json"
	"os"
)

// Config represents the application configuration
type Config struct {
	Server   ServerConfig   `json:"server"`
	Logging  LoggingConfig  `json:"logging"`
	Security SecurityConfig `json:"security"`
	API      APIConfig      `json:"api"`
	Redis    RedisConfig    `json:"redis"`
}

// ServerConfig holds the server configuration
type ServerConfig struct {
	Host        string `json:"host"`
	Port        string `json:"port"`
	StaticDir   string `json:"static_dir"`
	DataDir     string `json:"data_dir"`
	Environment string `json:"environment"`
	GinMode     string `json:"gin_mode"`
}

// LoggingConfig holds logging configuration
type LoggingConfig struct {
	Level        string `json:"level"`
	ConsoleLevel string `json:"console_level"`
	FileLevel    string `json:"file_level"`
	FilePath     string `json:"file_path"`
}

// SecurityConfig holds security related configuration
type SecurityConfig struct {
	EncryptionKey string `json:"encryption_key"`
}

// APIConfig holds API related configuration
type APIConfig struct {
	ClaudeBaseURL      string `json:"claude_base_url"`
	PlusBaseURL        string `json:"plus_base_url"`
	FingerprintBaseURL string `json:"fingerprint_base_url"`
}

// RedisConfig holds Redis related configuration
type RedisConfig struct {
	Host     string `json:"host"`
	Port     string `json:"port"`
	Password string `json:"password"`
	DB       int    `json:"db"`
}

// LoadConfig loads configuration from a JSON file
func LoadConfig(path string) (*Config, error) {
	file, err := os.Open(path)
	if err != nil {
		return nil, err
	}
	defer file.Close()

	var config Config
	decoder := json.NewDecoder(file)
	err = decoder.Decode(&config)
	if err != nil {
		return nil, err
	}

	return &config, nil
}
